package com.hx.springboot1.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.web.multipart.MultipartFile;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.Objects;
import java.util.Random;

/**
 * Author: hx
 * Date: 2021-05-15 13/51
 * Description: my mail Utils
 */
@Slf4j
@Async
public class MailUtils {
    @Autowired
    JavaMailSenderImpl mailSender;
    public void sendSimpleMail(String From,String To,String Subject,String Text){
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(From);
        message.setTo(To);
        message.setSubject(Subject);
        message.setText(Text);
        mailSender.send(message);
    }
    public Boolean sendMimeMail(String From,String To,String Subject,String Text,MultipartFile multiFile){
        MimeMessage message = mailSender.createMimeMessage();
        FileSystemResource file = new FileSystemResource(Objects.requireNonNull(this.MultipartFileToFile(multiFile)));
        String filename = file.getFilename();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(message,true);
            // from to
            helper.setFrom(From);
            helper.setTo(To);
            // subject text
            helper.setSubject(Subject);
            helper.setText(Text,true);
            // 附件
            helper.addAttachment(filename,file);
        } catch (MessagingException e) {
            log.info("邮件发送失败：{}",e.getMessage());
            return false;
        }
        mailSender.send(message);
        return true;
    }
    public Boolean sendMimeMail(String From,String To,String Subject,String Text){
        MimeMessage message = mailSender.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(message,true);
            // from to
            helper.setFrom(From);
            helper.setTo(To);
            // subject text
            helper.setSubject(Subject);
            helper.setText(Text,true);
        } catch (MessagingException e) {
            log.info("邮件发送失败：{}",e.getMessage());
            return false;
        }
        mailSender.send(message);
        return true;
    }
    public File MultipartFileToFile(MultipartFile multiFile) {
        // 获取文件名
        String name = multiFile.getOriginalFilename();
        // 获取文件后缀
        String suffix = name.substring(name.lastIndexOf("."));
        // 若需要防止生成的临时文件重复,可以在文件名后添加随机码
        Random ran = new Random();
        StringBuilder sb = new StringBuilder("name");
        String fileName = sb.append(ran.nextInt(100)).toString();
        try {
            File file = File.createTempFile(fileName, suffix);
            multiFile.transferTo(file);
            return file;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
